module.exports = app => {
    const mongoose = app.mongoose;
    const Schema = mongoose.Schema,
        ObjectId = Schema.Types.ObjectId;

    let MemberSchema = new Schema({
        email: { type: String, default: '' },  // 用户邮箱
        pass: { type: String, default: '' },  // 账号密码
        username: { type: String, default: '' },  // 用户名/昵称
        realname: { type: String, default: '' },  // 用户姓名
        mobile: { type: String, default: '' },  // 用户手机
        avatarsPath: { type: String, default: '/public/avatars/default.png' },  // 用户头像
        // roles: { type: ObjectId, ref: 'Role' },  // 角色
        grade: {type: Number, default: 0},  // 用户等级
        // permissions: ['*:*:*'],  // 权限列表
        // menus: [{ type: ObjectId, ref: 'Menu'}],  // 菜单列表
        articles: [{ type: ObjectId, ref: 'Article' }],  // 发表文章
        collect: [{ type: ObjectId, ref: 'Article' }],  // 收藏
        comments: [{ type: ObjectId, ref: 'Comment' }],  // 评论
        follow: [{ type: ObjectId, ref: 'User' }],  // 关注
        rewards: [{ type: ObjectId, ref: 'Article' }],  // ?
        status: { type: Number, default: 1 },  // 状态
        remark: { type: String, default: '' },  // 备注
        date: { type: Number, default: Date.now() }  // 时间
    }, {usePushEach: true});
    MemberSchema.statics = {
        saveMember: function(data) {
            return data.save();
        },
        findAll: function(cb) {
            return this.find({}).exec(cb);
        },
        findById: function(id, cb) {
            return this.findOne({_id: id}).exec(cb);
        },
        findByUnique: function(unique, cb) {
            return this.findOne(unique).exec(cb);
        },
        updateAvatars: function(id, avatarsPath, cb) {
          return this.findByIdAndUpdate({_id: id}, {$set: {avatarsPath: avatarsPath}}).exec(cb);
        },
        updateUser: function(uid, params, cb) {
          return this.findByIdAndUpdate({_id: uid}, {$set: params}, {new: true}, cb);
        },
        deleteUser: function(uid, cb) {
            return this.findByIdAndRemove(uid).exec(cb);
        },
        add_article(params) {
            return this.findByIdAndUpdate({_id: params.user_id}, {$push: {articles: params.article_id}}).exec();
        },
        del_article({uid, aid}) {
            return this.findByIdAndUpdate({_id: uid}, {$pull: {articles: aid}}).exec();
        },
        addCollect({uid, aid}, cb) {
            return this.findByIdAndUpdate({_id: uid}, {$push: {collect: aid}}, {new: true}, cb);
        },
        cutCollect({uid, aid}, cb) {
            return this.findByIdAndUpdate({_id: uid}, {$pull: {collect: aid}}, {new: true}, cb);
        }
    }

    return mongoose.model('Member', MemberSchema);

}